﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Entities;
using DAL;

namespace QuanLyBilliards_Coffee
{
    public partial class FormChiTietThongKe : Form
    {
        public FormChiTietThongKe()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormChiTietThongKe_Load(object sender, EventArgs e)
        {
            if(!Form1.tkthang)
            {
                LoadTheoNgay();
                
            }
            else
            {
                LoadTheoThang();
            }
        }

        private void LoadTheoNgay()
        {
            if (Form1.tk != "")
            {
                if (Form1.tktheoquy == true)
                {
                    ITKDoanhThuRepository tkdt = new TKDoanhThuRepository();
                    IQueryable<TKDoanhThu> listtk = tkdt.GetByThang(Form1.tk.Substring(Form1.tk.Length - 7, 7));
                    lvDSTKDoanhThu.Items.Clear();
                    LoadTKNgay(listtk);
                }
                else
                {
                    lvDSTKDoanhThu.Items.Clear();
                    ITKDoanhThuRepository tkdt = new TKDoanhThuRepository();
                    string quy = Form1.tk.Substring(4, Form1.tk.Length - 9);
                    string nam = Form1.tk.Substring(Form1.tk.Length - 4, 4);
                    if (quy == "I")
                    {
                        IQueryable<TKDoanhThu> listtk = tkdt.GetByThang("01/" + nam);
                        LoadTKNgay(listtk);
                        listtk = null;
                        listtk = tkdt.GetByThang("02/" + nam);
                        LoadTKNgay(listtk);
                        listtk = null;
                        listtk = tkdt.GetByThang("03/" + nam);
                        LoadTKNgay(listtk);
                    }
                    if (quy == "II")
                    {
                        IQueryable<TKDoanhThu> listtk = tkdt.GetByThang("04/" + nam);
                        LoadTKNgay(listtk);
                        listtk = null;
                        listtk = tkdt.GetByThang("05/" + nam);
                        LoadTKNgay(listtk);
                        listtk = null;
                        listtk = tkdt.GetByThang("06/" + nam);
                        LoadTKNgay(listtk);
                    }
                    if (quy == "III")
                    {
                        IQueryable<TKDoanhThu> listtk = tkdt.GetByThang("07/" + nam);
                        LoadTKNgay(listtk);
                        listtk = null;
                        listtk = tkdt.GetByThang("08/" + nam);
                        LoadTKNgay(listtk);
                        listtk = null;
                        listtk = tkdt.GetByThang("09/" + nam);
                        LoadTKNgay(listtk);
                    }
                    if (quy == "IV")
                    {
                        IQueryable<TKDoanhThu> listtk = tkdt.GetByThang("10/" + nam);
                        LoadTKNgay(listtk);
                        listtk = null;
                        listtk = tkdt.GetByThang("11/" + nam);
                        LoadTKNgay(listtk);
                        listtk = null;
                        listtk = tkdt.GetByThang("12/" + nam);
                        LoadTKNgay(listtk);
                    }
                    TinhThuChi();
                }
                Form1.tkmoi = "";
                Form1.tktheoquy = false;
            }
        }

        private void LoadTheoThang()
        {
            lvDSTKDoanhThu.Items.Clear();
            ITKDoanhThuRepository tkdt = new TKDoanhThuRepository();
            string quy = Form1.tk.Substring(4, Form1.tk.Length - 9);
            string nam = Form1.tk.Substring(Form1.tk.Length - 4, 4);
            if (quy == "I")
            {
                LoadTKThang("01/" + nam);
                LoadTKThang("02/" + nam);
                LoadTKThang("03/" + nam);
            }
            if (quy == "II")
            {
                LoadTKThang("04/" + nam);
                LoadTKThang("05/" + nam);
                LoadTKThang("06/" + nam);
            }
            if (quy == "III")
            {
                LoadTKThang("07/" + nam);
                LoadTKThang("08/" + nam);
                LoadTKThang("09/" + nam);
            }
            if (quy == "IV")
            {
                LoadTKThang("10/" + nam);
                LoadTKThang("11/" + nam);
                LoadTKThang("12/" + nam);
            }
            TinhThuChi();
            Form1.tkthang = false;
        }
        public void LoadTKNgay(IQueryable<TKDoanhThu> list)
        {
            foreach (var item in list)
            {
                ListViewItem it = lvDSTKDoanhThu.Items.Add(item.ThoiGianTK);
                it.SubItems.Add(item.SoTienThu.ToString());
                it.SubItems.Add(item.SoTienChi.ToString());
                it.SubItems.Add((item.SoTienThu - item.SoTienChi).ToString());
            }
            TinhThuChi();
        }
        public void LoadTKThang(string thang)
        {
            ITKDoanhThuRepository tk=new TKDoanhThuRepository();
            IQueryable<TKDoanhThu> listthang = tk.GetByThang(thang);
            int thu = 0, chi = 0;
            foreach (var item in listthang)
            {
                thu += item.SoTienThu;
                chi += item.SoTienChi;
            }
            ListViewItem it = lvDSTKDoanhThu.Items.Add(thang);
            it.SubItems.Add(thu.ToString());
            it.SubItems.Add(chi.ToString());
            it.SubItems.Add((thu-chi).ToString());
        }
        public void TinhThuChi()
        {
            int thu = 0, chi = 0, loinhuan = 0;
            for (int i = 0; i < lvDSTKDoanhThu.Items.Count; i++)
            {
                thu += int.Parse(lvDSTKDoanhThu.Items[i].SubItems[1].Text);
                chi += int.Parse(lvDSTKDoanhThu.Items[i].SubItems[2].Text);
            }
            loinhuan = thu - chi;
            txtLoiNhuan.Text = loinhuan.ToString();
            txtTongChi.Text = chi.ToString();
            txtTongThu.Text = thu.ToString();
        }
    }
}
